import { login } from '../apis/login';
import storageUtils from '../utils/storageUtils';

// const namespace = 'login';
// export const namespace_login = namespace;

export default {
  state: {
    token: '',
    user: {},
    message: null,
  },

  effects: {
    *loginSaga({ payload }, { call, put }) {
      const res = yield call(login, payload);
      const  { data: { token, user },message}=res
      // console.log(token,user,message);
      if (message === '认证成功') {
        storageUtils.setStorage('token', token);
        storageUtils.setStorage('userInfo', user);
      }
      yield put({
        type: 'setState',
        payload: {
          token,
          user,
          message,
        },
      });
    },
  },
  reducers: {
    setState(state, action) {
      return Object.assign({}, state, action.payload);
    },
  },
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     return history.listen(({ pathname }) => {
  //       if (pathname === '/login') {
  //         dispatch({
  //           type: 'query',
  //         });
  //       }
  //     });
  //   },
  // },
};
